#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 5e4 + 10;

int n, ind[50];
string s[N], t;

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	cin >> t; rep(i, 0, 25) ind[t[i] - 'a'] = i;
	n = in; rep(i, 1, n) { cin >> s[i]; for(auto &c : s[i]) c = ind[c - 'a'] + 'a'; }
	sort(s + 1, s + n + 1);
	rep(i, 1, n) {
		for(auto &c : s[i]) putchar(t[c - 'a']);
		puts("");
	} return 0;
}
